package com.hqd.jjvm.classformat.attributeinfo;

import com.hqd.jjvm.classformat.ClassFile;
import com.hqd.jjvm.util.HexStrTransformUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
@NoArgsConstructor
public class Exceptions extends AttributeInfo {
    private Integer numberOfExceptions;
    private List<Integer> exceptionIndexTable;

    public Exceptions(Integer attributeNameIndex, Integer attributeLength, Integer numberOfExceptions, List<Integer> exceptionIndexTable) {
        super(attributeNameIndex, attributeLength);
        this.numberOfExceptions = numberOfExceptions;
        this.exceptionIndexTable = exceptionIndexTable;
    }

    @Override
    protected Exceptions newAttributeInfo(Integer attributeNameIndex, Integer attributeLength, ClassFile classFile) {
        this.numberOfExceptions = HexStrTransformUtil.parseHexToInt(classFile.readU2());
        this.exceptionIndexTable = new ArrayList<>(numberOfExceptions);
        for (int i = 0; i < numberOfExceptions; i++) {
            exceptionIndexTable.add(HexStrTransformUtil.parseHexToInt(classFile.readU2()));
        }
        return this;
    }
}
